﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuantBox;
using SmartQuant;

namespace SgdDemo
{
    public class TradingTestStrategy : Strategy
    {
        private readonly Dictionary<int, Order> _openOrders;
        private Trade? _lastTrade;
        private int _index;
        public TradingTestStrategy(Framework framework, string name)
            : base(framework, name)
        {
            _openOrders = new Dictionary<int, Order>(100);
            AddReminder(Clock.DateTime.AddSeconds(5));
        }

        protected override void OnReminder(DateTime dateTime, object data)
        {
            if (_openOrders.Count > 0)
            {
                foreach (var item in _openOrders.ToArray())
                {
                    if (item.Value.IsDone)
                    {
                        _openOrders.Remove(item.Key);
                        continue;
                    }

                    if (!((Clock.DateTime - item.Value.DateTime).TotalSeconds > 5))
                    {
                        continue;
                    }
                    Cancel(item.Value);
                    item.Value.DateTime = DateTime.Now;
                }
            }
            AddReminder(Clock.DateTime.AddSeconds(5));
        }

        protected override void OnOrderRejected(Order order)
        {
            //base.OnOrderRejected(order);
        }

        protected override void OnOrderDone(Order order)
        {
            _openOrders.Remove(order.Id);
        }

        protected override void OnPendingNewOrder(Order order)
        {
            _openOrders.Add(order.Id, order);
        }

        protected override void OnTrade(Instrument instrument, Trade trade)
        {
            if (_lastTrade != null)
            {
                var position = this.GetPosition(instrument)!;
                if (position.LongTodayAvailable > 0)
                {
                    SellLimitOrder(instrument, 1, trade.Price, $"多头今仓出场_{_index}").CloseToday().Send();
                }
                else if (position.LongYesterdayAvailable > 0)
                {
                    SellLimitOrder(instrument, 1, trade.Price, $"多头出场_{_index}").Close().Send();
                }
                else if (position.ShortTodayAvailable > 0) 
                {
                    BuyLimitOrder(instrument, 1, trade.Price, $"空头今仓出场_{_index}").CloseToday().Send();
                }
                else if (position.ShortYesterdayAvailable > 0)
                {
                    BuyLimitOrder(instrument, 1, trade.Price, $"空头出场_{_index}").Close().Send();
                }
                else
                {
                    if (trade.Price > _lastTrade.Price)
                    {
                        BuyLimitOrder(instrument, 1, trade.Price, $"多头进场_{_index}").Send();
                    }
                    else
                    {
                        SellLimitOrder(instrument, 1, trade.Price, $"空头进场_{_index}").Send();
                    }
                }
                ++_index;
            }
            _lastTrade = trade;
        }
    }
}